package com.yzy.soft.system.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yzy.soft.common.persistence.dao.MetadataDatabaseMapper;
import com.yzy.soft.common.persistence.dao.MetadataTableMapper;
import com.yzy.soft.common.persistence.dao.TemplateJavaControllerMapper;
import com.yzy.soft.common.persistence.model.Maxno;
import com.yzy.soft.common.persistence.model.MetadataTable;
import com.yzy.soft.common.persistence.model.TemplateJavaController;
import com.yzy.soft.common.persistence.model.TypeMvc;
import com.yzy.soft.system.dto.CreateBackgroundFileDto;
import com.yzy.soft.system.freemarker.*;
import com.yzy.soft.system.freemarker.database.Column;
import com.yzy.soft.system.freemarker.database.PrimaryKey;
import com.yzy.soft.system.freemarker.database.Table;
import com.yzy.soft.system.service.CodeGenerateService;
import com.yzy.soft.system.util.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class CodeGenerateServiceImpl implements CodeGenerateService{

    private final static Logger logger= LoggerFactory.getLogger(CodeGenerateServiceImpl.class);
    @Autowired
    private MetadataDatabaseMapper metadataDatabaseMapper;
    @Autowired
    private MetadataTableMapper metadataTableMapper;

    @Autowired
    private TemplateJavaControllerMapper templateJavaControllerMapper;


    @Override
    public List<File> createFile(CreateBackgroundFileDto createBackgroundFileDto) {
        List<File> fileList=new ArrayList<>();
        logger.info("CodeGenerateServiceImpl : {}",createBackgroundFileDto);

        DBInfo dbInfo=metadataDatabaseMapper.findNecessaryInfo(createBackgroundFileDto.getDatabaseId());

        String tableid="("+createBackgroundFileDto.getTableIds()+")";
        createBackgroundFileDto.setTableIds(tableid);

//        List<MetadataTable> metadataTableList=metadataTableMapper.findByTableIds(tableid,createBackgroundFileDto.getDatabaseId());
        List<MetadataTable> metadataTableList=metadataTableMapper.findByTableIds(createBackgroundFileDto);


        TemplateJavaController templateJavaController = templateJavaControllerMapper.selectById(createBackgroundFileDto.getTemplateJavaController());

        List<String> list=new ArrayList<>();
        for(int j=0;j<metadataTableList.size();j++){

            MetadataTable metadataTable = metadataTableList.get(j);

            String className= StringUtil.replaceUnderLineAndUpperCase(metadataTable.getName());

            list.add(className);
        }
        String[] mvcTypes = createBackgroundFileDto.getMvcTypes();
        logger.info("mvcTypes:"+mvcTypes[0]);
        List<TypeMvc> typeMvcs=new ArrayList<>();
        for (int i = 0; i < mvcTypes.length; i++) {
            typeMvcs.add(new TypeMvc(mvcTypes[i]));
        }
//        Test test=new Test();
        CodeGenerateUtils codeGenerateUtils=new CodeGenerateUtils();

        codeGenerateUtils.setDbInfo(dbInfo);
        JdbcUtil jdbcUtil=new JdbcUtil(dbInfo.getDriver(),dbInfo.getUrl(),dbInfo.getUsername(),dbInfo.getPassword());
//

        for (int z = 0; z < list.size(); z++) {
            JavaFile javaFile = new JavaFile();
            javaFile.setPackageName("com.yzy.soft.system");
            javaFile.setDiskPath("D:\\BaiduYunDownload\\liscw-parent\\a\\");
            //
            JavaFileController javaFileController = new JavaFileController();
            javaFileController.setPackageName("com.yzy.soft.system");
            javaFileController.setAnnotation("测试");
            javaFileController.setFilename(list.get(z)+"Controller");
            javaFileController.setRequestMapping(list.get(z));
            javaFileController.setAuthor("yinzhiyuan");
            javaFile.setJavaFileController(javaFileController);

            //
            JavaFileDto javaFileDto = new JavaFileDto();
            javaFileDto.setPackageName("com.yzy.soft.system");
            javaFileDto.setFilename(list.get(z)+"Dto");
            javaFileDto.setRequestMapping(list.get(z));
            javaFileDto.setAuthor("yinzhiyuan");
            javaFile.setJavaFileDto(javaFileDto);

            //
            JavaFileModel javaFileModel = new JavaFileModel();
            javaFileModel.setPackageName("com.yzy.soft.system");
            javaFileModel.setFilename(list.get(z));
            javaFileModel.setRequestMapping(list.get(z));
            javaFileModel.setAuthor("yinzhiyuan");
            javaFile.setJavaFileModel(javaFileModel);

            //
            JavaFileService javaFileService = new JavaFileService();
            javaFileService.setPackageName("com.yzy.soft.system");
            javaFileService.setFilename(list.get(z)+"Service");
            javaFileService.setRequestMapping(list.get(z));
            javaFileService.setAuthor("yinzhiyuan");
            javaFile.setJavaFileService(javaFileService);


            //
            JavaFileServiceImpl javaFileServiceImpl = new JavaFileServiceImpl();
            javaFileServiceImpl.setPackageName("com.yzy.soft.system");
            javaFileServiceImpl.setFilename(list.get(z)+"ServiceImpl");
            javaFileServiceImpl.setRequestMapping(list.get(z));
            javaFileServiceImpl.setAuthor("yinzhiyuan");
            javaFile.setJavaFileServiceImpl(javaFileServiceImpl);

            //
            JavaFileDao javaFileDao = new JavaFileDao();
            javaFileDao.setPackageName("com.yzy.soft.system");
            javaFileDao.setFilename(list.get(z)+"Dao");
            javaFileDao.setRequestMapping(list.get(z));
            javaFileDao.setAuthor("yinzhiyuan");
            javaFile.setJavaFileDao(javaFileDao);

            JavaFileMapper javaFileMapper = new JavaFileMapper();
            javaFileMapper.setPackageName("com.yzy.soft.system");
            javaFileMapper.setFilename(list.get(z)+"Mappper");
            javaFileMapper.setRequestMapping(list.get(z));
            javaFileMapper.setAuthor("yinzhiyuan");
            javaFile.setJavaFileMapper(javaFileMapper);

            JavaFileMapperXml javaFileMapperXml = new JavaFileMapperXml();
            javaFileMapperXml.setPackageName("com.yzy.soft.system");
            javaFileMapperXml.setFilename(list.get(z)+"Mappper");
            javaFileMapperXml.setRequestMapping(list.get(z));
            javaFileMapperXml.setAuthor("yinzhiyuan");
            javaFile.setJavaFileMapperXml(javaFileMapperXml);
            codeGenerateUtils.setFileInfo(javaFile);


            Table table=new Table();
        table.setTableName(metadataTableList.get(z).getName());
        List<Column> columnsInfo = jdbcUtil.getColumnsInfoByTable(table.getTableName());

        table.setColumns(columnsInfo);
        List<PrimaryKey> primaryKeysInfo=jdbcUtil.getPrimaryKeysInfoByTable(table.getTableName());
        codeGenerateUtils.setTable(table);

            try {
//                fileList=codeGenerateUtils.generate(typeMvcs);
                fileList.addAll(codeGenerateUtils.generate(typeMvcs));
//            fileList.add(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileList;
    }

    @Override
    public boolean insert(Maxno maxno) {
        return false;
    }

    @Override
    public boolean insertAllColumn(Maxno maxno) {
        return false;
    }

    @Override
    public boolean insertBatch(List<Maxno> list) {
        return false;
    }

    @Override
    public boolean insertBatch(List<Maxno> list, int i) {
        return false;
    }

    @Override
    public boolean insertOrUpdateBatch(List<Maxno> list) {
        return false;
    }

    @Override
    public boolean insertOrUpdateBatch(List<Maxno> list, int i) {
        return false;
    }

    @Override
    public boolean insertOrUpdateAllColumnBatch(List<Maxno> list) {
        return false;
    }

    @Override
    public boolean insertOrUpdateAllColumnBatch(List<Maxno> list, int i) {
        return false;
    }

    @Override
    public boolean deleteById(Serializable serializable) {
        return false;
    }

    @Override
    public boolean deleteByMap(Map<String, Object> map) {
        return false;
    }

    @Override
    public boolean delete(Wrapper<Maxno> wrapper) {
        return false;
    }

    @Override
    public boolean deleteBatchIds(Collection<? extends Serializable> collection) {
        return false;
    }

    @Override
    public boolean updateById(Maxno maxno) {
        return false;
    }

    @Override
    public boolean updateAllColumnById(Maxno maxno) {
        return false;
    }

    @Override
    public boolean update(Maxno maxno, Wrapper<Maxno> wrapper) {
        return false;
    }

    @Override
    public boolean updateBatchById(List<Maxno> list) {
        return false;
    }

    @Override
    public boolean updateBatchById(List<Maxno> list, int i) {
        return false;
    }

    @Override
    public boolean updateAllColumnBatchById(List<Maxno> list) {
        return false;
    }

    @Override
    public boolean updateAllColumnBatchById(List<Maxno> list, int i) {
        return false;
    }

    @Override
    public boolean insertOrUpdate(Maxno maxno) {
        return false;
    }

    @Override
    public boolean insertOrUpdateAllColumn(Maxno maxno) {
        return false;
    }

    @Override
    public Maxno selectById(Serializable serializable) {
        return null;
    }

    @Override
    public List<Maxno> selectBatchIds(Collection<? extends Serializable> collection) {
        return null;
    }

    @Override
    public List<Maxno> selectByMap(Map<String, Object> map) {
        return null;
    }

    @Override
    public Maxno selectOne(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public Map<String, Object> selectMap(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public Object selectObj(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public int selectCount(Wrapper<Maxno> wrapper) {
        return 0;
    }

    @Override
    public List<Maxno> selectList(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public Page<Maxno> selectPage(Page<Maxno> page) {
        return null;
    }

    @Override
    public List<Map<String, Object>> selectMaps(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public List<Object> selectObjs(Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public Page<Map<String, Object>> selectMapsPage(Page page, Wrapper<Maxno> wrapper) {
        return null;
    }

    @Override
    public Page<Maxno> selectPage(Page<Maxno> page, Wrapper<Maxno> wrapper) {
        return null;
    }
}
